package com.hospital.ssm.interceptor;

import org.springframework.web.servlet.HandlerInterceptor;
import org.springframework.web.servlet.ModelAndView;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

public class LoginInterceptor implements HandlerInterceptor {

    @Override
    public boolean preHandle(HttpServletRequest request, HttpServletResponse response, Object handler) throws Exception {
        Object username = request.getSession().getAttribute("username");
        if (username == null) {
            response.sendRedirect(request.getContextPath() + "/loginui");
            return false;
        }
        return true;
    }

    // 在进入Controller后且返回视图前触发
    // 一般用来添加公共资源
    @Override
    public void postHandle(HttpServletRequest requet, HttpServletResponse response,
                           Object handler, ModelAndView mv)
            throws Exception {

    }


    // 在进入Controller后且返回视图后触发
    // 一般做日志收集、资源释放等操作
    @Override
    public void afterCompletion(HttpServletRequest requet, HttpServletResponse response,
                                Object handler, Exception e)
            throws Exception {

    }
}
